package STO_32_1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import LCommon.TreeNode;

public class Solution {
	public int[] levelOrder(TreeNode root) {
		
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		if (root==null) {
			return new int[0];
		}
		queue.add(root);
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (!queue.isEmpty()) {
			TreeNode headNode = queue.poll();
			list.add(headNode.val);
			if (headNode.left!=null) {
				queue.add(headNode.left);
			}
			if (headNode.right!=null) {
				queue.add(headNode.right);
			}
		}
		int[] res = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			res[i]=list.get(i);
		}
		
		return res;
	}
}
